function getUrlParam(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var result = document.location.search.substr(1).match(reg);
    return result ? encodeURI(result[2]) : null;
}

function getParameterByName(name, url) {
    if (!url) url = window.location.href;
    name = name.replace(/[\[\]]/g, "\\$&");
    var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
        results = regex.exec(url);
    if (!results) return null;
    if (!results[2]) return '';
    return decodeURIComponent(results[2].replace(/\+/g, " "));
}

$(function () {
     // return;

    window.localStorage.removeItem('backHref');
    window.localStorage.setItem('backHref', window.location.href);
    // return true;

    var openid = localStorage.getItem('openid');


    if (openid) {
        return;
    } else {

        $.ajax({
            url: '/fe/user/login',
            success: function (data) {
                console.log(data);
                location.href = data.url;
            },
            error: function (error) {
                console.log('no');
                console.log(error);
            }
        });
    }

})

function ajax(url, data) {
    var p = new Promise(function (resolve, reject) {
        $.ajax({
            url: url,
            type: data == null ? 'GET' : 'POST',
            dataType: "json",
            data: data == null ? '' : JSON.stringify(data),
            async: true,
            success: function (resp) {
                resolve(resp);
            },
            error: function (error) {
                reject(error);
            }
        });
    });
    return p;
}

function getCookie(name) {
    var arr = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)"));
    if (arr != null) return unescape(arr[2]);
    return null;
}

var dateRegex =
    /^(\d{4})-(\d{2})-(\d{2})[T ](\d{2}):(\d{2}):(\d{2})\.?(\d{3})?(?:(?:([+-]\d{2}):?(\d{2}))|Z)?$/;

function parseISODate(d) {
    var m = dateRegex.exec(d);
    if (m!=null){
        //milliseconds are optional.
        if (m[7] === undefined) {
            m[7] = 0;
        }

        //if timezone is undefined, it must be Z or nothing (otherwise the group would have captured).
        if (m[8] === undefined && m[9] === undefined) {
            //Use UTC.
            m[8] = 0;
            m[9] = 0;
        }

        var year = +m[1];
        var month = +m[2];
        var day = +m[3];
        var hour = +m[4];
        var minute = +m[5];
        var second = +m[6];
        var msec = +m[7];
        var tzHour = +m[8];
        var tzMin = +m[9];
        var tzOffset = tzHour * 60 + tzMin;

        //console.log(year+', '+(month - 1)+', '+day+', '+hour+', '+(minute - tzOffset)+', '+second+', '+msec);

        return new Date(year, month - 1, day, hour, minute - tzOffset, second, msec);
    }
    else return;
}


Date.prototype.format = function(fmt) {
     var o = {
        "M+" : this.getMonth()+1,                 //月份
        "d+" : this.getDate(),                    //日
        "h+" : this.getHours(),                   //小时
        "m+" : this.getMinutes(),                 //分
        "s+" : this.getSeconds(),                 //秒
        "q+" : Math.floor((this.getMonth()+3)/3), //季度
        "S"  : this.getMilliseconds()             //毫秒
    };
    if(/(y+)/.test(fmt)) {
            fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
    }
     for(var k in o) {
        if(new RegExp("("+ k +")").test(fmt)){
             fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
         }
     }
    return fmt;
}

Array.prototype.indexOf || (Array.prototype.indexOf = function(d, e) {
    var a;
    if (null == this) throw new TypeError('"this" is null or not defined');
    var c = Object(this),
        b = c.length >>> 0;
    if (0 === b) return -1;
    a = +e || 0;
    Infinity === Math.abs(a) && (a = 0);
    if (a >= b) return -1;
    for (a = Math.max(0 <= a ? a : b - Math.abs(a), 0); a < b;) {
        if (a in c && c[a] === d) return a;
        a++
    }
    return -1
});
